package com.mes.cloud.service.productpricegroup;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.productpricegroup.*;
import com.mes.cloud.api.dto.response.productpricegroup.ProductCustomerGroupDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.ProductCustomerGroupCustomerDao;
import com.mes.cloud.dao.ProductCustomerGroupDao;
import com.mes.cloud.entity.ProductCustomerGroup;
import com.mes.cloud.entity.ProductCustomerGroupCustomer;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.service.IPKeyGenerator;
import com.mes.cloud.service.SysCodeGeneratorService;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品客户组
 *
 * @author wxm
 * @version 2019-09-24 14:49:07
 * @email xiaomin.wu1@pactera.com
 */
@Service
public class ProductCustomerGroupService {

    private ProductCustomerGroupDao productCustomerGroupDao;

    private ProductCustomerGroupCustomerDao productCustomerGroupCustomerDao;

    private SysCodeGeneratorService sysCodeGeneratorService;

    private IPKeyGenerator iPKeyGenerator;

    @Autowired
    public ProductCustomerGroupService(ProductCustomerGroupDao productCustomerGroupDao, ProductCustomerGroupCustomerDao productCustomerGroupCustomerDao, SysCodeGeneratorService sysCodeGeneratorService, IPKeyGenerator iPKeyGenerator) {
        this.productCustomerGroupDao = productCustomerGroupDao;
        this.productCustomerGroupCustomerDao = productCustomerGroupCustomerDao;
        this.sysCodeGeneratorService = sysCodeGeneratorService;
        this.iPKeyGenerator = iPKeyGenerator;
    }

    /**
     * 分页查列表
     *
     * @param productCustomerGroupPageListRequest
     * @return JsonResult
     */
    public JsonResult<CommPager<ProductCustomerGroupDto>> pageList(ProductCustomerGroupPageListRequest productCustomerGroupPageListRequest) {
        Page<ProductCustomerGroupDto> page = PageHelper.startPage(productCustomerGroupPageListRequest.getCurrentPage(), productCustomerGroupPageListRequest.getPageSize());
        //查询条件
        ProductCustomerGroupListRequest productCustomerGroupListRequestParam = new ProductCustomerGroupListRequest();
        BeanUtils.copyProperties(productCustomerGroupPageListRequest, productCustomerGroupListRequestParam);
        List<ProductCustomerGroup> productCustomerGroupList = productCustomerGroupDao.list(productCustomerGroupListRequestParam);
        List<ProductCustomerGroupDto> productCustomerGroupDtoList = new ArrayList<>();
        for (ProductCustomerGroup productCustomerGroup : productCustomerGroupList) {
            ProductCustomerGroupDto productCustomerGroupDto = new ProductCustomerGroupDto();
            BeanUtils.copyProperties(productCustomerGroup, productCustomerGroupDto);
            productCustomerGroupDtoList.add(productCustomerGroupDto);
        }
        CommPager<ProductCustomerGroupDto> pageResult = new CommPager<>(page, productCustomerGroupDtoList);
        JsonResult<CommPager<ProductCustomerGroupDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }

    /**
     * 新增
     *
     * @param productCustomerGroupAddRequest
     * @return JsonResult
     */
    public JsonResult add(ProductCustomerGroupAddRequest productCustomerGroupAddRequest) {
        JsonResult jsonResult = new JsonResult();
        ProductCustomerGroup productCustomerGroup = new ProductCustomerGroup();
        BeanUtils.copyProperties(productCustomerGroupAddRequest, productCustomerGroup);
        productCustomerGroup.setCode(Constants.PRODUCT_CUSTOMER_GROUP_PREFIX + sysCodeGeneratorService.getCode(Constants.CODE_GENERATOR_PRODUCT_CUSTOMER_GROUP));
        productCustomerGroup.setId(iPKeyGenerator.getGenerateKey());
        //数据来源默认CRM
        productCustomerGroup.setSource(Constants.SOURCE_CRM);
        productCustomerGroup.setUpdateDateStr(DateUtil.updateDateStr());
        productCustomerGroup.setCreateBy(UserUtil.getCreateByName());
        productCustomerGroup.setUpdateBy(UserUtil.getUpdateByName());
        productCustomerGroup.setCreateByNo(UserUtil.getCreateByNo());
        productCustomerGroup.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = productCustomerGroupDao.insertSelective(productCustomerGroup);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 更新
     *
     * @param productCustomerGroupUpdateRequest
     * @return JsonResult
     */
    public JsonResult update(ProductCustomerGroupUpdateRequest productCustomerGroupUpdateRequest) {
        ProductCustomerGroup productCustomerGroup = new ProductCustomerGroup();
        BeanUtils.copyProperties(productCustomerGroupUpdateRequest, productCustomerGroup);
        productCustomerGroup.setUpdateDateStr(DateUtil.updateDateStr());
        productCustomerGroup.setUpdateBy(UserUtil.getUpdateByName());
        productCustomerGroup.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = productCustomerGroupDao.updateByPrimaryKeySelective(productCustomerGroup);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 查看详情
     *
     * @param id
     * @return JsonResult
     */
    public JsonResult<ProductCustomerGroupDto> get(String id) {
        JsonResult jsonResult = new JsonResult();
        ProductCustomerGroup productCustomerGroup = productCustomerGroupDao.selectByPrimaryKey(id);
        if (productCustomerGroup == null) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        //统计客户组下的客户数
        ProductCustomerGroupCustomer productCustomerGroupCustomerParam = new ProductCustomerGroupCustomer();
        productCustomerGroupCustomerParam.setProductCustomerGroupId(id);
        productCustomerGroupCustomerParam.setDelFlag(Constants.DEL_FLAG_NO);
        productCustomerGroup.setCustomerCount(productCustomerGroupCustomerDao.selectCount(productCustomerGroupCustomerParam));
        ProductCustomerGroupDto productCustomerGroupDto = new ProductCustomerGroupDto();
        BeanUtils.copyProperties(productCustomerGroup, productCustomerGroupDto);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(productCustomerGroupDto);
    }

    /**
     * 禁用启用
     *
     * @param id
     * @param enableFlag
     * @return JsonResult
     */
    public JsonResult updateEnableFlag(String id, String enableFlag) {
        ProductCustomerGroup productCustomerGroup = new ProductCustomerGroup();
        productCustomerGroup.setId(id);
        productCustomerGroup.setEnableFlag(enableFlag);
        int count = productCustomerGroupDao.updateByPrimaryKeySelective(productCustomerGroup);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 产品客户组批量启用禁用
     *
     * @param productCustomerGroupBatchEnableFlagRequest
     * @return
     */
    public JsonResult batchEnableFlag(ProductCustomerGroupBatchEnableFlagRequest productCustomerGroupBatchEnableFlagRequest) {
        JsonResult jsonResult = new JsonResult();
        productCustomerGroupDao.batchEnableFlag(productCustomerGroupBatchEnableFlagRequest.getEnableFlag(),
                productCustomerGroupBatchEnableFlagRequest.getIds(),
                DateUtil.updateDateStr(), UserUtil.getEmployeeNo(), UserUtil.getName());
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        return jsonResult;
    }
}
